﻿using B.S.Meeting.ErrorCode;
using B.S.Meeting.ReadAPI.Applications.Commands.MeetingRooms;
using B.S.Meeting.ReadAPI.Dtos;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.Meeting.ReadAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MeetingRoomController : ControllerBase
    {
        IMediator mediator;
        ILogger<MeetingRoomController> logger;

        public MeetingRoomController(IMediator mediator, ILogger<MeetingRoomController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 获取会议室列表
        /// </summary>
        /// <param name="cmd">查询条件</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<APIPaging<List<RoomDto>>>> GetRoom([FromQuery]GetRoomCommand cmd)
        {
            try
            {
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message, "获取会议室列表失败");
                throw;
            }
        }

        /// <summary>
        /// 获取会议室详情
        /// </summary>
        /// <param name="meetingRoomId">会议室ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<RoomDto>> GetRoomDetail([FromQuery] int meetingRoomId)
        {
            try
            {
                logger.LogInformation($"开始获取会议室详情: ID {meetingRoomId}");
                var command = new GetRoomDetailCommand { MeetingRoomId = meetingRoomId };
                var result = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "获取会议室详情失败");
                throw;
            }
        }
    }
}
